package com.ziyucoding.springboot.labs.lab10.seatdemo.atmd.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

/**
 * 商品 DAO
 *
 * @author ziyucoding
 * @date 2022-11-02
 **/
@Mapper
@Repository
public interface ProductDao {

    /**
     * 获取库存
     *
     * @param productId
     * @return Integer
     **/
    @Select("SELECT stock FROM product WHERE id = #{productId}")
    Integer getStock(@Param("productId") Long productId);

    /**
     * 扣减库存
     *
     * @param productId
     * @param amount
     * @return int
     **/
    @Update("UPDATE product SET stock = stock - #{amount} WHERE id = #{productId} AND stock >= #{amount}")
    int reduceStock(@Param("productId") Long productId, @Param("amount") Integer amount);

}
